/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;


import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1Trade;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Message_YN;
import l1j.server.server.utils.FaceToFace;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 處理收到由客戶端傳來交易的封包
 */
public class C_Trade extends ClientBasePacket {

	private static final String C_TRADE = "[C] C_Trade";
	private static final Logger _log = Logger.getLogger(C_Trade.class.getName());
	@Override
	public String getType() {
		return C_TRADE;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();

			if (pc==null ) {//opqlo 掛機檢測
				return;
			}
			
			if(pc.Canuse()){
				return;
			}
			
			if (pc.isGhost()) { // 鬼魂模式
				return;
			}
			if(pc.isInHell()){
				return;
			}

			if (pc.isDead()) { // 死亡
				return;
			}

			if (pc.isTeleport()) { // 傳送中
				return;
			}

			// 取回交易對象(舊的交易者)
			final L1Object srcTrade = L1World.getInstance().findObject(pc.getTradeID());

			if (srcTrade != null && srcTrade instanceof L1PcInstance) {  // 取消舊交易者交易狀態
				final L1Trade trade = new L1Trade();
				trade.TradeCancel(pc);
				return;
			}
			
			//TODO 修正角色人物不在螢幕內可進行交易的BUG
			final L1Character target = FaceToFace.faceToFace(pc);
			//TODO 修正角色人物不在螢幕內可進行交易的BUG
			
			if(target == null ){
				return;
			}
			if(!(target instanceof L1PcInstance)){
				return;
			}
			L1PcInstance tradePc = (L1PcInstance) target;
			if(tradePc.isTeleport()){
				return;
			}

			// 取回交易對象(接收指令者)
			final L1Object srcTradetarget = L1World.getInstance().findObject(tradePc.getTradeID());
			
			
			
			if (srcTradetarget != null && srcTradetarget instanceof L1PcInstance) {
				final L1Trade trade = new L1Trade();
				trade.TradeCancel((L1PcInstance) srcTradetarget);
				return;
			}
			if (!target.isParalyzed()) {
				pc.setTradeID(target.getId()); // 保存相互交易對象OBJID
				L1World.getInstance().addTradeInventory(pc.getId()).setPartnerId(target.getId());
				((L1PcInstance) target).setTradeID(pc.getId());
				L1World.getInstance().addTradeInventory(target.getId()).setPartnerId(pc.getId());
				((L1PcInstance) target).sendPackets(new S_Message_YN(252, pc.getName())); // %0%sがあなたとアイテムの取引を望んでいます。取引しますか？（Y/N）
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}

	}
}
